<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>表单校验</title>
  <link rel="stylesheet" href="css/style.css"></link>
</head>

<body>
<div class="login-form-wrap" style="width:100% ; text-align:center">
  <h1>黑马程序员</h1>
  <form class="login-form" action="#" id="regist" method="get" autocomplete="off" >
    <label>
      <input type="text" id="username" name="username" placeholder="Username..." value=""><br/>
    </label>
    <label>
      <input type="password" id="password" name="password" placeholder="Password..." value=""><br/>
    </label>
    <input type="submit" value="注册">
  </form>
</div>
</body>

<script>
      //1.绑定提交事件
      document.getElementById("regist").onsubmit=function (){

        //2.获取填写用户和密码
        let username = document.getElementById("username").value;
        let password = document.getElementById("password").value;

        //3.判断用户名
        let reg1=/^[a-zA-Z]{4,16}$/;
        if(!reg1.test(username)){
          alert("用户名不符合规则")
          return false;
        }

        //4.判断密码
        let reg2=/^[\d]{6}$/;
        if (!reg2.test(password)){
          alert("密码不符合规则")
          return false;
        }

        //条件都不满足
        return true;
      }

</script>
</html>